<template>
  <div class="app-container">
    <div style="margin-bottom: 4px;">
      <el-button type="primary" size="mini" @click="handleAdd()">新增</el-button>
      <el-button type="danger" size="mini" @click="handleDeleteAll()">删除</el-button>
    </div>
    <el-table ref="multipleTable" :data="list" v-loading="listLoading" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="Code">
        <template slot-scope="scope">
          {{scope.row.roleCode}}
        </template>
      </el-table-column>
      <el-table-column label="Name">
        <template slot-scope="scope">
          <span>{{scope.row.roleName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handlePermission(scope.$index, scope.row)">权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 4px;">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :page-size="pageSize"
        :current-page="pageNum"
        :total="pageTotal"
        @current-change="pageChange">
      </el-pagination>
    </div>

    <el-dialog :title="dialogFormTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="编号" :label-width="formLabelWidth">
          <el-input v-model="form.roleCode" :disabled="codeDisabled"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="权限设置" :visible.sync="dialogPermissionVisible">
      <el-tree
        :data="permissionList"
        node-key="id"
        show-checkbox
        default-expand-all
        check-strictly
        :expand-on-click-node="false"
        ref="tree">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span class="custom-tree-span">{{ data.perName }}</span>
        【<span  class="custom-tree-span">{{ data.perCode }}</span>】
      </span>
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPermissionVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPermission()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, add, update, deleted, getPermission, getPermissionList, setPermission } from '@/api/role'

export default {
  data() {
    return {
      pageNum: 1, // 当前页
      pageSize: 20, // 每页数
      pageTotal: 0, // 总页数
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogFormTitle: '',
      formLabelWidth: '100px',
      form: {},
      codeDisabled: true,
      dialogPermissionVisible: false,
      permissionList: null,
      roleId: ''
    }
  },
  filters: {
  },
  created() {
    this.fetchData()
    this.initPermissionTree()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.pageNum, this.pageSize).then(response => {
        console.log(response)
        this.list = response.list
        this.listLoading = false
        this.pageTotal = response.total
        this.pageNum = response.pageNum
      })
    },
    initPermissionTree() {
      getPermissionList().then(response => {
        console.log(response)
        this.permissionList = response.data
      })
    },
    handleAdd() {
      this.form = {}
      this.codeDisabled = false
      this.dialogFormTitle = '新增'
      this.dialogFormVisible = true
    },
    handleDeleteAll() {
      console.log(this.$refs.multipleTable.selection)
      const selectedData = this.$refs.multipleTable.selection
      if (selectedData.length <= 0) {
        this.$message({
          type: 'info',
          message: '请选择至少一行数据'
        })
        return
      }
      let ids = ''
      for (let i = 0; i < selectedData.length; i++) {
        ids += selectedData[i].id + ','
      }
      if (ids.length > 0) {
        ids = ids.substring(0, ids.length - 1)
      }
      this.$confirm('确认删除选中的' + selectedData.length + '条数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(ids).then(response => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.fetchData()
        })
      })
    },
    handleEdit(index, row) {
      this.form = row
      this.codeDisabled = true
      this.dialogFormTitle = '编辑'
      this.dialogFormVisible = true
    },
    handleDelete(index, row) {
      this.$confirm('确认删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleted(row.id + '').then(response => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.fetchData()
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    handlePermission(index, row) {
      this.roleId = row.id
      this.dialogPermissionVisible = true
      getPermission(row.id).then(response => {
        const list = response.data
        const keys = []
        for (let i = 0; i < list.length; i++) {
          keys.push(list[i].permissionId)
        }
        this.$refs.tree.setCheckedKeys(keys)
      })
    },
    submit() {
      console.log(this.form)
      if (this.form.id && this.form.id !== '') {
        // 编辑
        update(this.form).then(response => {
          this.dialogFormVisible = false
        })
      } else {
        // 新增
        add(this.form).then(response => {
          this.dialogFormVisible = false
          this.fetchData()
        })
      }
    },
    submitPermission() {
      const keys = this.$refs.tree.getCheckedKeys()
      let ids = ''
      for (let i = 0; i < keys.length; i++) {
        ids += keys[i] + ';'
      }
      if (ids.length > 0) {
        ids = ids.substring(0, ids.length - 1)
      }
      setPermission(this.roleId, ids).then(response => {
        this.dialogPermissionVisible = false
        this.$message({
          type: 'success',
          message: '修改权限成功'
        })
      })
    },
    pageChange(val) {
      this.pageNum = val
      this.fetchData()
    }
  }
}
</script>

<style>
  .custom-tree-node {
    width: 100%;
  }
  .custom-tree-span {
    height: 28px;
    display: inline-block;
    padding-top: 4px;
  }
</style>
